﻿using FastCloud.Base.Config;
using FastIOC.Attribute;
using FastORM.Entity;
using FastORM.Mapping;
using FastUtil.Config;
using FastUtil.Convert;
using FastUtil.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.Base.Desgin
{
    public class BaseCacheML<TEntity> : BaseML<TEntity> where TEntity : BaseEntity
    {
        private string TableCacheKey { set; get; }
        public BaseCacheML()
        {
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            TableCacheKey = mapper.TableName;
        }

        public void InitCache()
        {
            CommonDTO BaseDTO = new CommonDTO();
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            List<TEntity> DBEntityList = BaseDTO.Select<TEntity>(mapper.TableName, "*", "1=1", "");
            RedisUtil.DelteKey(TableCacheKey, CloudUtil.GetRedisCacheDBNum());
            foreach (TEntity Entity in DBEntityList)
            {
                string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
                RedisUtil.SetHash(TableCacheKey, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
            }
        }

        public override void Insert(TEntity Entity)
        {
            base.Insert(Entity);
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
            RedisUtil.SetHash(TableCacheKey, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
        }

        public override void InsertBatch(List<TEntity> EntityList)
        {
            base.InsertBatch(EntityList);
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            foreach (TEntity Entity in EntityList)
            {
                string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
                RedisUtil.SetHash(TableCacheKey, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
            }
        }

        public override void DeleteByID(string UnitGuid)
        {
            base.DeleteByID(UnitGuid);  
            RedisUtil.DelteHash(TableCacheKey, UnitGuid, CloudUtil.GetRedisCacheDBNum());
        }

        public override void DeleteBatch(List<string> UnitGuidList)
        {
            base.DeleteBatch(UnitGuidList);
            foreach (string UnitGuid in UnitGuidList)
            {
                RedisUtil.DelteHash(TableCacheKey, UnitGuid, CloudUtil.GetRedisCacheDBNum());
            }
        }

        public override void UpdateByID(TEntity Entity)
        {
            base.UpdateByID(Entity);
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
            RedisUtil.SetHash(TableCacheKey, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
        }

        public override void UpdateBatch(List<TEntity> EntityList)
        {
            base.UpdateBatch(EntityList);
            MapperEntity mapper = StructMapping.GetInstance().GetMapperEntity(typeof(TEntity));
            foreach (TEntity Entity in EntityList)
            {
                string RowGuid = ValueConvert.ConvertToString(mapper.GetKeyValue(Entity));
                RedisUtil.SetHash(TableCacheKey, RowGuid, JsonConvert.SerializeObject(Entity), CloudUtil.GetRedisCacheDBNum());
            }
        }

        public override TEntity SelectOne(string UnitGuid)
        {
            string result = RedisUtil.GetHash(TableCacheKey, UnitGuid, CloudUtil.GetRedisCacheDBNum());
            if (!string.IsNullOrEmpty(result))
            {
                return JsonConvert.DeserializeObject<TEntity>(result);
            }
            else
            {
                return base.SelectOne(UnitGuid);
            }
        }

    }
}
